package com.uclick.chat2;

import org.apache.log4j.Logger;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Vector;

public class ChatServer {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(ChatServer.class);

	Vector<Data> buffer;
	ServerSocket serverSocket;
	Socket socket;
	ObjectInputStream ois;
	ObjectOutputStream oos;

	public void service() {
		if (logger.isDebugEnabled()) {
			logger.debug("service() - start"); //$NON-NLS-1$
		}
		
		try {
			serverSocket = new ServerSocket(3333);
		} catch (IOException e) {
			logger.warn("service() - exception ignored", e); //$NON-NLS-1$
		}
		while (true) {
			try {
				socket = serverSocket.accept();
//				System.out.println(socket.getInputStream());
				String ip = socket.getInetAddress().getHostAddress();

				ois = new ObjectInputStream(socket.getInputStream());
				oos = new ObjectOutputStream(socket.getOutputStream());
				Thread t = new Thread(new ChatServerThread(buffer, ois, oos,ip));
				t.start();
			} catch (IOException e) {
				logger.error("service()", e); //$NON-NLS-1$

				e.printStackTrace();
			}
			if (logger.isDebugEnabled()) {
				logger.debug("service() - end"); //$NON-NLS-1$
			}
		}
	}

	public static void main(String args[]) {
		System.out.println("start Server service...");
		ChatServer cs = new ChatServer();
		cs.buffer = new Vector<Data>(5, 1);
		cs.service();
	}
}
